<template>
    <view>
        <view class="searchbox">
            <view  class="nav-card top-fix">
                <block v-for="(item, index) in navigationList" :key="index">
                    <view :class="'index ' + (currentId == item.id ? 'active' : 'default')" @tap="selShopTc" :id="item.id">
                        <text>{{ item.name }}</text>
                    </view>
                </block>
            </view>
        </view>

        <!-- 搜索组件 -->
        <view class="search-wrapper">
            <view class="searchimg">
                <image class="img" src="https://xxzhang.oss-cn-hangzhou.aliyuncs.com/%E6%90%9C%E7%B4%A2.png"></image>
            </view>
            <view class="search-input">
                <input maxlength="30" placeholder="搜索 讲师/课件 关键字" @input="inputChange" />
            </view>
            <view class="search-button" @tap="clickbutton">
                <image class="img" src="https://xxzhang.oss-cn-hangzhou.aliyuncs.com/%E6%90%9C%E7%B4%A2%E6%8C%89%E9%92%AE.png"></image>
            </view>
        </view>

        <!-- 下方列表 -->
        <view class="appointment">
            <block v-for="(courseware, index) in courseware" :key="index">
                <navigator url="url" :id="courseware.id" @tap="details" class="mod-item">
                    <view class="mod-img">
                        <image :src="courseware.userHeadImg" class="mod-image"></image>
                        <view class="head-text">
                            <text>{{ courseware.title }}</text>
                            <view class="mod-info">
                                <view v-if="courseware.classification == 5" class="type"><text style="font-size: 28rpx">五年级</text></view>
                                <view v-if="courseware.classification == 7" class="type"><text style="font-size: 28rpx">七年级</text></view>
                                <view v-if="courseware.classification == 9" class="type"><text style="font-size: 28rpx">图片回顾</text></view>
                                <view v-if="courseware.classification == 11" class="type"><text style="font-size: 28rpx">视频回顾</text></view>
                             <!--   <view class="moinfont">{{ courseware.likeNum }}人观看</view> -->
                            </view>
                        </view>
                    </view>
                </navigator>
            </block>
        </view>

 

     <view class="btn" v-if="showToTop" @tap="gotop">
     	<image style="width:45rpx;height:45rpx;" src="/static/iconfont/backToTop.png"></image>
         <text style="color: white;font-size:smaller;">顶部</text>
     </view>
    </view>
</template>

<script>
// pages/searchfor/searchfor.js
const app = getApp();
var urlPrefix = app.globalData.urlPrefix;
export default {
    data() {
        return {
            search: '',
            currentId: 1,
            courseware: [],
            navigationList: [],
            showToTop: false,
			
			
			token:null
        };
		
    },
	
	
 
    onLoad(options) {
		var token = uni.getStorageSync('token');
		this.token = token
		uni.showLoading({
		      title: '加载中',
		});
        let that = this;
        uni.request({
            url: urlPrefix + '/api/applet/index/searchList',
            method: 'GET',
            header: {
                'content-type': 'application/json',
                'token': this.token
            },
            success: function (res) {
                let dataList = res.data;
                that.setData({
                    navigationList: dataList.datas
                });
            }
        });

		
		//课件
		uni.request({
		    url: urlPrefix + "/api/applet/index/coursewareListIndex?type=1",
		    method: 'GET',
		    header: {
		        'content-type': 'application/json',
				 'token': this.token
		    },
		    success: function (res) {
		        let dataList = res.data;
		        that.setData({
		            courseware: dataList.datas,
		        });
				uni.hideLoading();
		    }
		});
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
		uni.stopPullDownRefresh();  //停止刷新
		
	},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
	
	},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {},
    onShareTimeline(res) {
        // 分享到朋友圈
        return {};
    },
    /**
     * 用户滚动页面时
     */
    onPageScroll(e) {
        if (e.scrollTop > 780) {
            this.setData({
                showToTop: true
            });
        } else {
            this.setData({
                showToTop: false
            });
        }
    },
    methods: {

		
		
        inputChange(e) {
            let that = this;
            that.setData({
                search: e.detail.value
            });
        },

        details: function (res) {
            var id = res.currentTarget.id;
            uni.navigateTo({
                url: '/pages/coursewaredetails/coursewaredetails?id=' + id
            });
        },

        clickbutton: function (e) {
			uni.showLoading({
			      title: '正在搜索',
			});
            let that = this;
            uni.request({
                url: urlPrefix + '/api/applet/index/coursewareList?type=' + this.currentId + '&search=' + this.search,
                method: 'GET',
                header: {
                 'content-type': 'application/json',
                   'token': this.token
                },
                success: function (suss) {
                    let dataList = suss.data;
                    that.setData({
                        courseware: dataList.datas
                    });
					uni.hideLoading();
                }
            });
        },

        selShopTc: function (res) {
			uni.showLoading({
			      title: '加载中',
			});
           let that = this;
           uni.request({
               url: urlPrefix + '/api/applet/index/coursewareListIndex?type=' + res.currentTarget.id,
               method: 'GET',
               header: {
                   'content-type': 'application/json',
				    'token': this.token
               },
               success: function (suss) {
                   let dataList = suss.data;
                   that.setData({
                       currentId: res.currentTarget.id,
                       courseware: dataList.datas
                   });
               }
           });
		   uni.hideLoading();
        },

        gotop: function (e) {
            if (uni.pageScrollTo) {
                uni.pageScrollTo({
                    scrollTop: 0,
                    //滚到顶部
                    duration: 0.5 //滚的到顶部需要的时间  动画
                });
            } else {
                uni.showModal({
                    title: '提示',
                    content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
                });
            }
        }
    }
};
</script>
<style lang="scss">
@import './searchfor.scss';
</style>
